/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* SListinit()
{
	struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
	tmp->next = NULL;
	return tmp;
}
struct ListNode* BuyNewNode(int data)
{
	struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
	tmp->val = data;
	tmp->next = NULL;
    return tmp;
}
void SListPushBack(struct ListNode** List,int x)
{
	if (*List == NULL)
	{
		*List = SListinit();
		(*List)->val = x;
		return;
	}
	else
	{
		struct ListNode* tail = *List;
		struct ListNode* tmp = BuyNewNode(x);
		if (tmp == NULL)
		{
			printf("开辟空间失败!");
			return;
		}
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = tmp;
	}
}
int cmp_int(const void* buf1,const void* buf2)
{
    return *(int*)buf1 - *(int*)buf2;
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize)
{
    int arr[10000];
    int count =0;
    struct ListNode* new = NULL;
    while(listsSize--)
    {
        struct ListNode* cur = *(lists+listsSize);
        while(cur)
        {
            *(arr+count) = cur->val;
            cur =cur->next;
            count++;
        }
    }
    if(count ==0)
    {
        return new;
    }
    qsort(arr,count,4,cmp_int);
    for(int i=0;i<count;i++)
    {
        SListPushBack(&new,arr[i]);
    }
    return new;
}